'''
Created on 27/09/2012

@author: greis
'''
import shop

def shopSmart(orders,shops):
    """
        takes an orderList (like the kind passed in to FruitShop.getPriceOfOrder) 
        and a list of FruitShop and returns the FruitShop where your order costs the least amount in total. 
    """
    totalCosts = []
    for shop in shops:
        totalCosts.append((shop,shop.getPriceOfOrder(orders)))
    return quickSort(totalCosts,1)[0][0]

def quickSort(lst, index):
    """
        sort a List of Tuples by the element in the Tuple informed in index
    """
    if len(lst) <= 1: 
        return lst
    smaller = [x for x in lst[1:] if x[index] < lst[0][index]]
    larger = [x for x in lst[1:] if x[index] >= lst[0][index]]
    return quickSort(smaller,index) + [lst[0]] + quickSort(larger,index)
